package com.guaoran.jvm.newObject;

/**
 * @author : guaoran
 * @Description
 *  空间分配担保
 * @date :2019/3/1 15:28
 */
public class SpaceGuarantee {
    public static void main(String[] args) {
        //-verbose:gc -XX:+PrintGCDetails -Xms20M -Xmx20M -Xmn10M
        byte[] d1 = new byte[2 * 1024 * 1024];
        byte[] d2 = new byte[2 * 1024 * 1024];
        byte[] d3 = new byte[2 * 1024 * 1024];
        byte[] d4 = new byte[4 * 1024 * 1024];
        System.gc();
    /*
    [GC (Allocation Failure) [PSYoungGen: 6605K->952K(9216K)] 6605K->5056K(19456K), 0.0061140 secs] [Times: user=0.03 sys=0.00, real=0.01 secs]
    [GC (System.gc()) --[PSYoungGen: 7335K->7335K(9216K)] 11439K->13487K(19456K), 0.0245714 secs] [Times: user=0.00 sys=0.00, real=0.02 secs]
    [Full GC (System.gc()) [PSYoungGen: 7335K->4825K(9216K)] [ParOldGen: 6152K->6145K(10240K)] 13487K->10970K(19456K), [Metaspace: 3245K->3245K(1056768K)], 0.0138269 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
    Heap
     PSYoungGen      total 9216K, used 5126K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
      eden space 8192K, 62% used [0x00000000ff600000,0x00000000ffb01b00,0x00000000ffe00000)
      from space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
      to   space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
     ParOldGen       total 10240K, used 6145K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
      object space 10240K, 60% used [0x00000000fec00000,0x00000000ff200548,0x00000000ff600000)
     Metaspace       used 3265K, capacity 4496K, committed 4864K, reserved 1056768K
      class space    used 356K, capacity 388K, committed 512K, reserved 1048576K
          class space    used 359K, capacity 388K, committed 512K, reserved 1048576K

      todo 从上面可以看出来 Eden 区占有 62%，old区占有60%，并触发了GC
      todo 因为新生代是10M，Eden ：s0:s1=8:1:1,所以，Eden=8M，s0,s1=1M,
      todo 当Eden分配了三个2M的内存后，再分配4M的内存将不够分配，则会触发GC，
      todo GC的时候，会将Eden区的数据分配到s0,但是s0只有1M，无法存储Eden的数据，则会将已分配到Eden区的6M内存移动到老年代，在分配4M的数据到Eden区
     */
    }
}
